<?php


class PropertyService extends TP_Service {
    private $role;
    public function __construct() {
        parent::__construct();
        $this->load->model("SModel/Property_model");
        $this->load->service("PService/UploadService");

        $this->load->service("PService/ExportExcelService");

        $this->role = $this->getRole($_SESSION[SESS_UUID]);
    }



    /**
     * 方法名 propertyList
     * 
    * @Name propertyList
    * @Desc 字段列表
    * @OpenFlag 是否开放,1开放0权限控制
    * @Author      DJC
    * @Date 2022-02-21 14:17:09
    * @return      [type]                   [description]
    */
    public function propertyList($param,$export = false){
        if (!$export) {
            $param = array_filter($param, 'filtrfunction');
            if (!isset($param['pageNo'], $param['pageSize'])) {
                exit(retJson(ErrCode::ErrCode_10002));
            }

            $conarr['pageNo'] = $param['pageNo'];
            $conarr['pageSize'] = $param['pageSize'];
        }
        $conarr['where'] = "1=1";

        if($export){
            $list = $this->Property_model->get_list_full($conarr);
        }else{
            $list = $this->Property_model->get_page_list($conarr);

            foreach($list['ret'] as $k => &$v ){
                $v['search_char'] = $v['is_search'] == 1 ? '是':'否';
                $v['multiple_char'] = $v['is_multiple'] == 1?'多选':'单选';
                $v['status_char'] = $v['is_status'] == 1 ? '禁用':'启用';
                $v['customer_char'] = $v['is_customer'] == 1 ? '隐藏':'显示';
                $v['required_char'] = $v['is_required'] == 1 ? '必填':'选填';
                $options = json_decode($v['value'],true);
                $v['value'] = "";
                $v['rid'] =$options['rid'];
                if($options['rid'] == 0){
                    foreach($options['options'] as $value){
                        $v['value'] .= empty($v['value']) ? $value : ','.$value;
                    }
                }else{
                    foreach($options['options'] as $k1 =>  $value){
                        $v['valueP'][$k1] = "";
                        foreach($value as $v1){
                            $v['value'] .= empty($v['value']) ? $v1 : ','.$v1;
                            $v['valueP'][$k1] .= empty($v['valueP'][$k1]) ? $v1 : ','.$v1;
                        }
                    }
                }
            }
        }

      



        return $list;

    }


    public function saveProperty($param){
        if(!isset(
            $param['key'],
            $param['key_name'],
            $param['value'],
            $param['is_search'],
            $param['is_multiple'],
            $param['is_status'],
            $param['is_customer'],
            $param['is_required'],


        )){
            exit(retJson(ErrCode::ErrCode_10002));
        }



        $options = explode(',',$param['value']);

        $value = array(
            'options' => $options,
            'rid' => 0
        );


        $data = array(
            'key' => $param['key'],
            'key_name' => $param['key_name'],
            'value' =>  json_encode($value,JSON_UNESCAPED_UNICODE),
            'is_search' => $param['is_search'],
            'is_multiple' => $param['is_multiple'],
            'is_status' => $param['is_status'],
            'is_customer' => $param['is_customer'],
            'is_required' => $param['is_required'],

        );


        if(isset($param['id'])){
            $this->Property_model->update($data,array('id' => $param['id']));
        }else{
            $data['created'] = date('Y-m-d H:i:s');
            $this->Property_model->add($data);
        }
        return true;
    }




    public function property(){
        $conarr['items'] = "id,key_name,value";
        $conarr['where'] = "1=1";
        $list = $this->Property_model->get_list_full($conarr);
        
        $ret = array();
        foreach($list as $k => &$v){
            $v['value'] = json_decode($v['value'],true);
            if($v['value']['rid'] == 0){
                $ret[] = [
                    'key_name' => $v['key_name'],
                    'id' => (int) $v['id'],
                    'value' => $v['value']['options']
                ];
            }
        }
        return $ret;
    }



    
    /**
     * 方法名 saveProRelation
     * 
    * @Name saveProRelation
    * @Desc 保存关联字段
    * @OpenFlag 是否开放,1开放0权限控制
    * @Author      DJC
    * @Date 2022-02-21 14:17:09
    * @return      [type]                   [description]
    */
    public function saveProRelation($param){
        if(!isset(
            $param['key'],
            $param['key_name'],
            $param['valueP'],
            $param['rid'],
            $param['is_search'],
            $param['is_multiple'],
            $param['is_status'],
            $param['is_customer'],
            $param['is_required']
        )){
            exit(retJson(ErrCode::ErrCode_10002));
        }


        $options = array();
        foreach($param['valueP'] as $k => $v){
            $options[] = explode(',',$v);
        }


        $value = array(
            'options' => $options,
            'rid' => $param['rid']
        );


        $data = array(
            'key' => $param['key'],
            'key_name' => $param['key_name'],
            'value' =>  json_encode($value,JSON_UNESCAPED_UNICODE),
            'is_search' => $param['is_search'],
            'is_multiple' => $param['is_multiple'],
            'is_status' => $param['is_status'],
            'is_customer' => $param['is_customer'],
            'is_required' => $param['is_required'],

        );


        if(isset($param['id'])){
            $this->Property_model->update($data,array('id' => $param['id']));
        }else{
            $data['created'] = date('Y-m-d H:i:s');
            $this->Property_model->add($data);
        }
        return true;
    }



    /**
     * 方法名 function
     * 
    * @Name 获取方法列和角色方法列
    * @Desc 方法描述
    * @OpenFlag 是否开放,1开放0权限控制
    * @Author      DJC
    * @Date 2022-02-21 16:54:16
    * @return      [type]                   [description]
    */
    public function delProperty($param){
        if(!isset($param['id'])){
            exit(retJson(ErrCode::ErrCode_10002));
        }

        $this->Property_model->delete(array('id' => $param['id']));

        return true;
    }



    /**
     * 方法名 importProperty
     * 
    * @Name importProperty
    * @Desc 导入字段
    * @OpenFlag 是否开放,1开放0权限控制
    * @Author      DJC
    * @Date 2022-02-21 16:54:16
    * @return      [type]                   [description]
    */
    public function importProperty($param = array()){
        if (empty($_FILES)) {
            exit(retJson(ErrCode::ErrCode_10601));
        }


        $path = $this->UploadService->UploadFile('Property', true);
        $excellist =  $this->ExportExcelService->importexcel($path, 'H', 'A', 2);
        if (count($excellist) == 0) {
            exit(retJson(ErrCode::ErrCode_10602));
        }

        //去掉空数组
        foreach ($excellist as &$value) {
            $value = array_filter($value, 'filtrfunction');
        }
        $excellist = array_filter($excellist);

        // var_dump($excellist);

        $data = [];
        foreach ($excellist as $v) {
            $options = explode(',',$v['C']);

            $valueP = ["options" => $options,"rid" => 0];
            $data[] = array(
                'key_name' => $v['A'],
                'key' => $v['B'],
                'is_multiple' => $v['D'] == "单选" ? '0' :'1',
                'is_search' => $v['E'] == "是" ? '1' :'0',
                'is_status' => $v['F'] == "禁用" ? '1' :'0',
                'is_customer' => $v['G'] == "隐藏" ? '1' :'0',
                'is_required' => $v['H'] == "必填" ? '1' :'0',
                'value' => json_encode($valueP,JSON_UNESCAPED_UNICODE)
            );
        }

        $this->Property_model->add($data);


        return true;

    }



}